// Copyright 2023 IOsetting <iosetting(at)outlook.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/***
 * Demo:  TM1640 16x8 LED Driver
 * 
 *    Pin connection:
 *    P35           => DIN
 *    P32           => SCK
 *                     VDD      => 3.3V - 5V
 *                     GND      => GND
 * 
 * test-board: Minimum System; test-MCU: STC8H1K08
 */

#include "fw_hal.h"
#include "tm1640.h"

uint8_t numbers[]={
0x00,0x00,0x7C,0xC6,0xC6,0xCE,0xD6,0xD6,  // -0-.  
0xE6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,  // -1-  
0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00,  
0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30,  // -2-  
0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00,  
0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06,  // -3-  
0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,  
0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE,  // -4-  
0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00,  
0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x0E,  // -5-  
0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,  
0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6,  // -6-  
0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,  
0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18,  // -7-  
0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,  
0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6,  // -8-  
0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00, 
0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06,  // -9-  
0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00};


const uint8_t alph[]={
  0x3e,0x63,0x63,0x7f,0x63,0x63,0x63,0x63, //A
  0x7e,0x63,0x63,0x7e,0x63,0x63,0x63,0x7e, //B
  0x3e,0x63,0x63,0x60,0x60,0x63,0x63,0x3e, //C
  0x7e,0x63,0x63,0x63,0x63,0x63,0x63,0x7e, //D
  0x7f,0x60,0x60,0x7f,0x60,0x60,0x60,0x7f, //E
  0x7f,0x60,0x60,0x7e,0x60,0x60,0x60,0x60, //F
  0x3e,0x63,0x63,0x60,0x67,0x63,0x63,0x3e, //G
  0x63,0x63,0x63,0x7f,0x63,0x63,0x63,0x63, //H
  0x3f,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3f, //I
  0x1f,0x06,0x06,0x06,0x06,0x66,0x66,0x3c, //J
  0x63,0x66,0x6c,0x78,0x6c,0x66,0x63,0x61, //K
  0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x7f, //L
  0x63,0x77,0x7f,0x6b,0x63,0x63,0x63,0x63, //M
  0x63,0x63,0x73,0x7b,0x6f,0x67,0x63,0x63, //N
  0x3e,0x63,0x63,0x63,0x63,0x63,0x63,0x3e, //O
  0x7e,0x63,0x63,0x63,0x7e,0x60,0x60,0x60, //P
  0x3c,0x66,0x66,0x66,0x66,0x6e,0x66,0x3f, //Q
  0x7e,0x63,0x63,0x63,0x7e,0x6c,0x66,0x63, //R
  0x3e,0x63,0x63,0x60,0x3e,0x03,0x63,0x3e, //S
  0x3f,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, //T
  0x63,0x63,0x63,0x63,0x63,0x63,0x63,0x3e, //U
  0x63,0x63,0x63,0x63,0x63,0x36,0x1c,0x08, //V
  0x63,0x63,0x63,0x63,0x6b,0x7f,0x77,0x63, //W
  0x63,0x63,0x36,0x1c,0x1c,0x36,0x63,0x63, //X
  0x33,0x33,0x33,0x33,0x1e,0x0c,0x0c,0x0c, //Y
  0x7f,0x03,0x06,0x0c,0x18,0x30,0x60,0x7f, //Z
  0x3e,0x63,0x73,0x6b,0x67,0x63,0x63,0x3e, //0
  0x0c,0x1c,0x3c,0x0c,0x0c,0x0c,0x0c,0x3f, //1
  0x3e,0x63,0x63,0x06,0x0c,0x18,0x30,0x7f, //2
  0x3e,0x63,0x63,0x0e,0x03,0x63,0x63,0x3e, //3
  0x06,0x0e,0x1e,0x36,0x66,0x7f,0x06,0x06, //4
  0x7f,0x60,0x60,0x7e,0x03,0x03,0x03,0x7e, //5
  0x3e,0x63,0x60,0x7e,0x63,0x63,0x63,0x3e, //6
  0x7f,0x03,0x03,0x06,0x0c,0x18,0x18,0x18, //7
  0x3e,0x63,0x63,0x3e,0x63,0x63,0x63,0x3e, //8
  0x3e,0x63,0x63,0x63,0x3f,0x03,0x63,0x3e, //9
};


void GPIO_Init(void)
{
    // SCLK(P32) CSN(P35)
    GPIO_P3_SetMode(GPIO_Pin_2|GPIO_Pin_5, GPIO_Mode_Output_PP);
}

uint8_t revert(uint8_t in)
{
    uint8_t out = 0, i;
    for (i = 0; i < 8; i++)
    {
        if (in & 0x01)
        {
            out = out | 0x01;
        }
        out = out << 1;
        in = in >> 1;
    }
    return out;
}

int main(void)
{
    uint8_t i = 0;
    SYS_SetClock();
    GPIO_Init();

    // UART1, baud 115200, baud source Timer1, 1T mode, no interrupt
    UART1_Config8bitUart(UART1_BaudSource_Timer1, HAL_State_ON, 115200);
    UART1_TxString("UART Initialized\r\n");

    /* Set brightness */
    TM1640_WriteCommand(TM1640_DISPLAY_ON | TM1640_BRIGHT_0);

    TM1640_ResetAll(0xFF);
    SYS_Delay(1000);
    TM1640_ResetAll(0x00);
    SYS_Delay(1000);

    for (i = 0; i < sizeof(numbers); i++)
    {
        numbers[i] = revert(numbers[i]);
    }
    i = 0;

    while (1)
    {
        TM1640_SetAddrIncrMode();
        while(i < (sizeof(numbers) / 8 - 1))
        {
            TM1640_WriteRange(0x00, numbers + 8 * i, 16);
            i++;
            SYS_Delay(500);
        }
        i = 1;
        while(i)
        {
            TM1640_ResetAll(i++);
            SYS_Delay(100);
        }
    }
}
